package com.ceba.test.modules.admin.helper;

import com.github.hui.quick.plugin.base.GraphicUtil;
import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
import com.google.zxing.WriterException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * https://liuyueyi.github.io/quick-media/#/%E6%8F%92%E4%BB%B6/%E4%BA%8C%E7%BB%B4%E7%A0%81/%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C
 * @author leeguang
 */
public class QrCodePlugin {

    static Logger logger = LoggerFactory.getLogger(QrCodePlugin.class);

    /**
     *
     * @param qrContent 二维码内容
     * @param word 前景文字，当前文字在二维码的上面。"扫码查看在线报告"
     * @param logoImgPath logo图片，加在二维码的正中间
     * @param detectImgPath 左上、右上、左下三个定位码眼的图片
     * @param outImgPath 二维码输出文件路径
     * @return
     */
    public static boolean encoderQRCode(String qrContent,
                                        String word,
                                        String logoImgPath,
                                        String detectImgPath,
                                        String outImgPath) {
//        BufferedImage ftImg = createFtImg(word);

        QrCodeGenWrapper.Builder b = QrCodeGenWrapper.of(qrContent);
        try {
            b.setLogo(logoImgPath)
                .setLogoBgColor(0xfffefefe)
                .setLogoBorderBgColor(0xfffefefe)
                .setLogoBorder(true)
//                .setW(142)
//                .setH(160)
                .setDetectImg(detectImgPath);
//                .setFtImg(ftImg)
//                .setFtFillColor(Color.WHITE)
//                .setFtStartX(0)
//                .setFtStartY(10);
            return b.asFile(outImgPath);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("generate nuoan qr link fail,qrContent = " + qrContent);
        } catch (WriterException e) {
            e.printStackTrace();
            logger.error("generate nuoan qr link fail,qrContent = " + qrContent);
        }

        return false;
    }

    /**
     * 创建一个文字的前置图
     */
    private static BufferedImage createFtImg(String content) {
        BufferedImage ftImg = GraphicUtil.createImg(140, 160, null);
        Graphics2D g2d = GraphicUtil.getG2d(ftImg);
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 160, 140, 140);
        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font(null, Font.PLAIN, 16));
        g2d.drawString(content, 6, 16);

        return ftImg;
    }

    public static void main(String[] args) throws IOException, WriterException {
//        BufferedImage ftImg = createFtImg("扫码查看在线报告");

        String imgPath = "E:\\ceba\\test\\qrcode\\qrcode.png";
        String detectImgPath = "E:\\ceba\\test\\qrcode\\detect.png";
        String logoImagePath = "E:\\ceba\\test\\qrcode\\logo_rongda.png";
//        String encoderContent = "https://www.baidu.com";
        String encoderContent = "https://coa.mxns.com.cn/qr/12345678901234567890123456789021";
//        String encoderContent = "https://www.merieuxnutrisciences.com.cn/sites/merieux_nutrisciences_cn/files/atoms/files/03978b0f-82f4-4282-b503-4d004489709e.pdf";
//        String encoderContent = "https://www.merieuxnutrisciences.com.cn/sites/12345678901234567890123456789021.pdf";
        // 生成二维码，并输出为qr.png图片
        QrCodeGenWrapper.Builder b = QrCodeGenWrapper.of(encoderContent);
        b.setLogo(logoImagePath)
            .setLogoBgColor(0xfffefefe)
            .setLogoBorderBgColor(0xfffefefe)
            .setLogoBorder(true)
//            .setW(142)
//            .setH(160)
            .setDetectImg(detectImgPath);
//            .setFtImg(ftImg)
//            .setFtFillColor(Color.WHITE)
//            .setFtStartX(0)
//            .setFtStartY(10);
        boolean ans = b.asFile(imgPath);
    }
}
